//
//  ViewController.m
//  UI11_UITableView编辑_cell删除添加_cell移动
//
//  Created by Scott on 16/3/23.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "TableViewOfContact.h"
#import "VCOfSecond.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) TableViewOfContact *tableView;

@property (nonatomic, retain) NSMutableArray *mArr;
@end

@implementation ViewController

- (void)dealloc {
    [_tableView release];
    [_mArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"TablView编辑";
    
    [self handleData];
    [self createTableView];
    
    // 利用系统的提供的一个editItem, 并且已经封装好的action(setEditing:animated:)
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(handleNext:)] autorelease];
}

#pragma mark - 跳转下一页
- (void)handleNext:(UIBarButtonItem *)button {
    
    VCOfSecond *second = [[VCOfSecond alloc] initWithStyle:UITableViewStyleGrouped];
    
    [self.navigationController pushViewController:second animated:YES];
    
    [second release];
}

#pragma mark - 编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
    [super setEditing:editing animated:animated];
    
    // 让tableView 进入/退出 编辑状态.
    [self.tableView setEditing:editing  animated:animated];

}

#pragma mark - Data
- (void)handleData {
    
    self.mArr = @[@"zhangsan", @"lisi", @"wangwu", @"Tom"].mutableCopy;
    
}

#pragma mark - tableView
- (void)createTableView {
    
    self.tableView = [[TableViewOfContact alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
}


#pragma mark - 知识点1: tableView编辑(插入,删除)
/** 
 * 思路: 
 *  1. 让tableView进入编辑状态
 *  2. 允许哪些行可以编辑(默认是所有行) (API:DataSource)
 *  3. 设置每行的编辑样式(删除还是插入) (API: Delegate)
 *  4. 提交编辑 (API: DataSource)
 */

#pragma mark - 知识点2: tableView移动
/** 
 * 思路: 
 *  1. tableView进入编辑状态
 *  2. 哪些行可以移动(API: DataSource)
 *  3. 移动(API: DataSource)
 */


#pragma mark - tableViewDataSourse
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.mArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *pool = @"pool";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:pool] autorelease];
    }
    
    cell.textLabel.text = [self.mArr objectAtIndex:indexPath.row];
    
    return cell;
}

/** 哪些行可以编辑. */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 除了第一行, 其它行可以编辑
    if (indexPath.row == 0) {
        return NO;
        
    } else {
        
        return YES;
    }

}

/** 提交编辑. */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 当点击删除
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        // 更新数据
        [self.mArr removeObjectAtIndex:indexPath.row];

        // 删除相应的row
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
        // 点击插入
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        
        // 更新数据
        [self.mArr insertObject:@"lili" atIndex:indexPath.row];
        
        
        // 插入相应的row
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

/** 哪些行可以移动. */
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

    return YES;

}

/** 移动. */
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    
    // 更新数据
    NSString *source = [[self.mArr objectAtIndex:fromIndexPath.row] retain];
    
    [self.mArr removeObjectAtIndex:fromIndexPath.row];
    
    [self.mArr insertObject:source atIndex:toIndexPath.row];
    
    [source release];
}

#pragma mark - tableView Delegate
/** 每行编辑风格. */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 第二行是插入, 其它是删除.
    if (indexPath.row == 1) {
        return UITableViewCellEditingStyleInsert;
        
    } else {
        
        return UITableViewCellEditingStyleDelete;
    }
    
}




#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
